package com.kania.simpleemail.data;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import javax.mail.Part;

public class MailData {

	private ArrayList<String> alFrom = new ArrayList<String>();
	private ArrayList<String> alTo = new ArrayList<String>();
	private ArrayList<String> alCC = new ArrayList<String>();
	private ArrayList<String> alBCC = new ArrayList<String>();
	private Calendar date;
	private String subject = null;
	private long uid;

	//[130731]메일 내용은 추후 MIME을 지원하도록 바꿔야 한다.(get set도 추후 삭제)
	private String contents = "";
	
	private ArrayList<String> alAttach = new ArrayList<String>();
	private HashMap<String, Part> hashAttact = new HashMap<String, Part>();

	public MailData () {
		date = Calendar.getInstance();
		uid = 0;
	}
	
	public MailData (String _from, String _to, String _cc, String _bcc, Calendar _date, String _subject){
		setFromData(_from);
		setToData(_to);
		setCCData(_cc);
		setBCCData(_bcc);
		date = _date;
		subject = _subject;
		uid = 0;
	}

	public long getUID () {
		return uid;
	}

	public void setUID (long _uid) {
		uid = _uid;
	}

	public String getMailDate() {
		return date.get(Calendar.YEAR) + "-" + 
				(date.get(Calendar.MONTH)+1) + "-" + 
				date.get(Calendar.DATE) + " " + 
				date.get(Calendar.HOUR_OF_DAY) + ":" + 
				date.get(Calendar.MINUTE);
	}
	
	public void setMailDate(String sDate) {
		String sSplitedDate[] = sDate.split(" ");
		String sDays[] = sSplitedDate[0].split("-");
		String sTimes[] = sSplitedDate[1].split(":");
		date.set(Calendar.YEAR, Integer.parseInt(sDays[0]));
		date.set(Calendar.MONTH, Integer.parseInt(sDays[1]));
		date.set(Calendar.DATE, Integer.parseInt(sDays[2]));
		date.set(Calendar.HOUR, Integer.parseInt(sTimes[0]));
		date.set(Calendar.MINUTE, Integer.parseInt(sTimes[1]));
	}

	public void setMailSubject(String _subject) {
		subject = _subject;
	}
	
	public String getMailSubject() {
		return subject;
	}

	public void setContents(String _contents) {
		contents = _contents;
	}
	public String getContents() {
		return contents;
	}

	//To. 관련 메서드 정의
	public void addToData(String _to) {
		alTo.add(_to);
	}
	public void setToData(String _to) {
		alTo.clear();
		String[] sToDatas = _to.split(",");
		for(int i = 0; i < sToDatas.length; i++) {
			alTo.add(sToDatas[i]);
		}
	}
	public String getToData() {
		String sAllToData = "";
		for(String s: alTo){
			sAllToData += s + ", ";
		}
		return sAllToData;
	}
	public int getToCount() {
		return alTo.size();
	}

	//From. 관련 메서드 정의
	public void addFromData(String _from) {
		alFrom.add(_from);
	}
	public void setFromData(String _from) {
		alFrom.clear();
		String[] sFromDatas = _from.split(",");
		for(int i = 0; i < sFromDatas.length; i++) {
			alFrom.add(sFromDatas[i]);
		}
	}
	public String getFromData() {
		String sAllFromData = "";
		for(String s: alFrom){
			sAllFromData += s + ", ";
		}
		return sAllFromData;
	}

	//CC. 관련 메서드 정의
	public void addCCData(String _cc) {
		alCC.add(_cc);
	}
	public void setCCData(String _cc) {
		alCC.clear();
		String[] sCCDatas = _cc.split(",");
		for(int i = 0; i < sCCDatas.length; i++) {
			alCC.add(sCCDatas[i]);
		}
	}
	public String getCCData() {
		String sAllCCData = "";
		for(String s: alCC){
			sAllCCData += s + ", ";
		}
		return sAllCCData;
	}

	//BCC. 관련 메서드 정의
	public void addBCCData(String _bcc) {
		alBCC.add(_bcc);
	}
	public void setBCCData(String _bcc) {
		alBCC.clear();
		String[] sBCCDatas = _bcc.split(",");
		for(int i = 0; i < sBCCDatas.length; i++) {
			alBCC.add(sBCCDatas[i]);
		}
	}
	public String getBCCData() {
		String sAllBCCData = "";
		for(String s: alBCC){
			sAllBCCData += s + ", ";
		}
		return sAllBCCData;
	}
	
	public void addAttachFileName(String _filename) {
		alAttach.add(_filename);
	}
	
	public ArrayList<String> getAttachArrayList() {
		return alAttach;
	}
}